
const { Connection } = require('@solana/web3.js');
const config = require('./config');
class SolChain {
    constructor(nodeUrl) {
        this.nodeUrl = nodeUrl;
        this.connection = new Connection(this.nodeUrl);
    }
    async getBalance(pubKey) {
        let balance = await this.connection.getBalance(pubKey);
        return balance;
    }
    async getBlockHeight() {
        let height = await this.connection.getSlot({commitment:"finalized"});
        return height;
    }
    async getBlock(slot) {
        let block = await this.connection.getBlock(slot, {commitment: "finalized", maxSupportedTransactionVersion: 0, rewards:false});
        return block;

    }
    async getBlocks(startSlot, endSlot) {
        let blocks = await this.connection.getBlocks(startSlot, endSlot, {commitment: "finalized", maxSupportedTransactionVersion: 0, rewards:false});
        return blocks;

    }
    async getTransaction(signature) {
        let tx = await this.connection.getTransaction(signature, {commitment:"finalized", maxSupportedTransactionVersion: 0});
        return tx;
    }
    async sendTransaction(transaction, keypair) {
        let ret = await this.connection.sendTransaction( transaction, [keypair]);
        return ret;
    }
    async sendRawTransaction(rawTx) {
        let ret = await this.connection.sendRawTransaction(rawTx);
        return ret;
    }
    async getLatestBlockhash() {
        return await this.connection.getLatestBlockhash();
    }
    async getAccountInfo(publicKey) {
        return await this.connection.getAccountInfo(publicKey);
    }
    async getSignaturesForAddress(programPubKey,until){
        return await this.connection.getSignaturesForAddress(programPubKey,{ until:until},"finalized");
        
    }


}

module.exports = SolChain;
